var date = new Date();
var m = date.getMonth();
var y = date.getFullYear();
daysInMonth = function(year,month){
return new Date(year,month + 1,0).getDate();
};

calendar=function(src,year,month){
var k = 1;
if(month && year){
date.setFullYear(year,month,1);
}
else{
date.setFullYear(date.getFullYear(),date.getMonth(),1);
}
month = date.getMonth();
year = date.getFullYear();
days = daysInMonth(year,month);
lastMonthDays = daysInMonth(year,month - 1);
month_arr = ["Jan.","Feb.","Mar.","Apr.","May","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec."];
str = '';
str += "<table>";
str += "<tr><td><input type='image' id='"+UID(src)+"y_prev' src='../../images/y_prev.png'/><\/td><td><input type='image' id='"+UID(src)+"m_prev' src='../../images/m_prev.png'/><\/td><td colspan='3'>"+month_arr[month]; 
str += "&nbsp;<select id='"+UID(src)+"select_yr'>";
for(var i=(year-30);i<(year+30);i++){
selected = (i == year)?("selected=true"):(null);
str += "<option "+selected+" value='"+i+"'>"+i+"<\/option>";
}
str += "<\/select>";
str += "<\/td><td> <input type='image' id='"+UID(src)+"m_next' src='../../images/m_next.png'/><\/td><td><input type='image' id='"+UID(src)+"y_next' src='../../images/y_next.png'/><\/td><\/tr>";
str += "<tr><td>S<\/td><td>M<\/td><td>T<\/td><td>W<\/td><td>T<\/td><td>F<\/td><td>S<\/td><\/tr>";
for(var i=0;i<6;i++){
str += "<tr>";
for(var j=1;j<=7;j++){
cal = (j + (i * 7) - date.getDay());
if(cal <= 0){
str += "<td><a href='#' style='color:#E8E8E8'>"+(lastMonthDays+cal)+"<\/a><\/td>";
}
else if(cal > days){
str += "<td><a href='#' style='color:#E8E8E8'>"+k+"<\/a><\/td>";
k++;
}
else{
mark = (cal == new Date().getDate())?("background:#f5f5f5"):(null);
str += "<td style='"+mark+"'><a href='#'>"+cal+"<\/a><\/td>";
}
}
str += "<\/tr>";
if (cal >= days && i != 6){
			break;
			}
}
str += "<\/table>";
var obj=document.createElement("div");
obj.className="calendar";
obj.id=UID(src)+"calendar";
obj.innerHTML=str;

if(arguments.length==1){
	toggleFunc(UID(src),function(){src.parentNode.appendChild(obj);},function(){src.parentNode.removeChild($(UID(src)+"calendar"));});
}
else{
	src.parentNode.appendChild(obj);
}


a_arr = obj.getElementsByTagName("a");
for(var $i=0;$i<a_arr.length;$i++){
a_arr[$i].onclick=function(){
src.value=zerofill(this.innerHTML)+"/"+zerofill(month+1)+"/"+zerofill(year);
src.parentNode.removeChild($(UID(src)+"calendar"));
};
}

$(UID(src)+'select_yr').onchange=function(){
	y=this.options[this.selectedIndex].value;
	src.parentNode.removeChild($(UID(src)+"calendar"));
	src.parentNode.appendChild(calendar(src,y,m));
};
$(UID(src)+"m_next").onclick=function(){
m++;
if(m > 12){
m = 1;
y++;
}
src.parentNode.removeChild($(UID(src)+"calendar"));
src.parentNode.appendChild(calendar(src,y,m));
};
$(UID(src)+"m_prev").onclick=function(){
m--;
if(m < 1){
m = 12;
y--;
}
src.parentNode.removeChild($(UID(src)+"calendar"));
src.parentNode.appendChild(calendar(src,y,m));
};
$(UID(src)+"y_next").onclick=function(){
y++;
src.parentNode.removeChild($(UID(src)+"calendar"));
src.parentNode.appendChild(calendar(src,y,m));
};
$(UID(src)+"y_prev").onclick=function(){
y--;
src.parentNode.removeChild($(UID(src)+"calendar"));
src.parentNode.appendChild(calendar(src,y,m));
};
};